﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;

namespace PlanningDepartment
{
    public partial class FrHuManResourcePlanning : Telerik.WinControls.UI.RadForm
    {
        public FrHuManResourcePlanning()
        {
            InitializeComponent();
        }
         private SqlConnection Conn;
        private Process prs = new Process();

        private DataSet ds = new DataSet();
      
        private int i = 0, j = 0, k = 0, count = 0, int_temp = 0;
        private double d = 0, temp_double = 0;
        private string string_temp = null,BacLuong1;
        #region //Class Public
        private string Date_Split(string Date)
        {
            string Ngay_split;
            string[] temp = Date.Split('/');
            Ngay_split = temp[1] + "/" + temp[0] + "/" + temp[2];
            return Ngay_split;
        }
        private void ChangeThemeName(Control control, string themeName)
        {
            IComponentTreeHandler radControl = control as IComponentTreeHandler;
            if (radControl != null) { radControl.ThemeName = themeName; }
            foreach (Control child in control.Controls)
            { ChangeThemeName(child, themeName); }
        }
        private void Mni_Office2010Black_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2010Black.Text);
        }

        private void Mni_Office2010Blue_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2010Blue.Text);
        }

        private void Mni_Office2017Black_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2017Black.Text);
        }

        private void Mni_Office2007Silver_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2007Silver.Text);
        }

        private void Mni_Office2010Silver_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2010Silver.Text);
        }

        private void Mni_Windows7_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Windows7.Text);
        }
        private void Fr_Main_Load(object sender, EventArgs e)
        {
            ChangeThemeName(this, "Office2010Blue");
            Load_Form();
        }
        private void checktext_number(RadTextBox txt)
        {
            try
            {
                if (txt.Text.Length > 0)
                {
                    txt.Text = string.Format("{0:###,###,###,###.##}", double.Parse(txt.Text));//ép kiểu float,nếu xảy ra ngoại lệ thì bẫy lỗi ở hàm checktext_number
                    txt.SelectionStart = txt.Text.Length;
                }
            }
            catch (Exception ex)//Bẫy lỗi khi nhập chữ vào text box
            {
                string s = "Input string";
                if (ex.Message.ToString().Contains(s))
                {
                    MessageBox.Show("Giá trị nhập phải là số");
                    txt.Text = "";
                }
                else
                    MessageBox.Show(ex.Message);
            }
        }
        private string Ngay_Split(string Date)
        {
            string Ngay_split;
            string[] temp = Date.Split('/');
            Ngay_split = temp[1] + "/" + temp[0] + "/" + temp[2];
            temp.Clone();
            return Ngay_split;
        }
        private void Fr_Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (e.KeyData == Keys.F11)
            {
                if (PageChild_ThongTinCNV.SelectedPage==PageChild_CNV)
                {
                    bt_SaveTTNV_Click(null, null);
                }
            }
        }
        private void Load_Form()
        {
            string[] arr_Tinh = new string[] { "An Giang", "Bà Rịa-Vũng Tàu", "Bạc Liêu", "Bắc Kạn", "Bắc Giang", "Bắc Ninh", "Bến Tre", "Bình Dương", "Bình Định", "Bình Phước", "Bình Thuận", "Cà Mau", "Cao Bằng", "Cần Thơ (TP)", "Đà Nẵng (TP)", "Đắk Lắk", "Đắk Nông", "Điện Biên", "Đồng Nai", "Đồng Tháp", "Gia Lai", "Hà Giang", "Hà Nam", "Hà Nội (TP)", "Hà Tây", "Hà Tĩnh", "Hải Dương", "Hải Phòng (TP)", "Hòa Bình", "Hồ Chí Minh (TP)", "Hậu Giang", "Hưng Yên", "Khánh Hòa", "Kiên Giang", "Kon Tum", "Lai Châu", "Lào Cai", "Lạng Sơn", "Lâm Đồng", "Long An", "Nam Định", "Nghệ An", "Ninh Bình", "Ninh Thuận", "Phú Thọ", "Phú Yên", "Quảng Bình", "Quảng Nam", "Quảng Ngãi", "Quảng Ninh", "Quảng Trị", "Sóc Trăng", "Sơn La", "Tây Ninh", "Thái Bình", "Thái Nguyên", "Thanh Hóa", "Thừa Thiên - Huế", "Tiền Giang", "Trà Vinh", "Tuyên Quang", "Vĩnh Long", "Vĩnh Phúc", "Yên Bái" };
            for (j = 0; j < arr_Tinh.Length; j++)
            {
                cb_ThanhPho.Items.Add(arr_Tinh[j].ToString());
                cb_QueQuan.Items.Add(arr_Tinh[j].ToString());


            }
            arr_Tinh.Clone();
            arr_Tinh = new string[] { "ABBank - An Bình", "NASBank, NASB - Bắc Á", "VIET CAPITAL BANK, VCCB - Bản Việt", "BaoVietBank, BVB - Bảo Việt", "LienVietPostBank - Bưu Điện Liên Việt", "Vietinbank - Công thương", "Dai A Bank - Đại Á", "Oceanbank - Đại Dương", "Trustbank - Đại Tín", "GP.Bank - Dầu khí Toàn Cầu", "BIDV - Đầu tư phát triển Việt Nam", "DongA Bank, DAB - Đông Á", "SeABank - Đông Nam Á", "Maritime Bank, MSB - Hàng Hải Việt Nam", "KienLongBank - Kiên Long", "Techcombank - Kỹ Thương Việt Nam", "Nam A Bank - Nam Á", "NaViBank - Nam Việt", "Sacombank - Ngân hàng Sài Gòn Thương Tín", "Vietcombank - Ngoại thương", "Agribank - Nông nghiệp", "MDB - Phát Triển Mê Kông", "MHB - Phát triển Nhà Đồng bằng sông Cửu Long", "HDBank - Phát Triển Nhà Thành phố Hồ Chí Minh", "Orient Commercial Bank, OCB - Phương Đông", "Southern Bank, PNB - Phương Nam", "Western Bank - Phương Tây", "Military Bank, MB, - Quân Đội", "VIBBank, VIB - Quốc tế", "Sài Gòn, SCB - Sài Gòn", "Saigonbank - Sài Gòn Công Thương", "SHBank, SHB - Sài Gòn-Hà Nội", "TienPhongBank - Tiên Phong", "VietABank, VAB - Việt Á", "VPBank - Việt Nam Thịnh Vượng", "VietBank - Việt Nam Thương Tín", "Petrolimex Group Bank, PG Bank - Xăng dầu Petrolimex", "Eximbank, EIB - Xuất nhập khẩu" };

            for (j = 0; j < arr_Tinh.Length; j++)
            {
                cb_NganHang.Items.Add(arr_Tinh[j].ToString());
            }
            arr_Tinh.Clone();
            arr_Tinh = new string[] { "Kinh", "Ba- Na", "Bố Y", "Brâu", "Bru-Vân Kiều", "Chăm (Chàm)", "Chơ-ro", "Chu-ru", "Chứt", "Co", "Cơ Lao", "Cơ-ho", "Cống", "Cơ-tu", "Dao", "Ê-đê", "Gia-rai", "Giáy", "Gié-Triêng", "Hà Nhì", "Hoa (Hán...)", "Hrê", "Kháng", "Khơme", "Khơ-mú", "La Chí", "La Ha", "La Hủ", "Lào", "Lô Lô", "Lự", "Mạ", "Mảng", "Mnông", "Mông", "Mường", "Ngái", "Nùng", "Ơ-đu", "Pà Thẻn", "Phù Lá", "Pu Péo", "Ra-glai", "Rơ-măm", "Sán Chay (Cao Lan - Sán Chỉ)", "Sán Dìu", "Si La", "Ta-ôi", "Tày", "Thái", "Thổ (3)", "Xinh-mun", "Xơ-đăng", "Xtiêng" };
            for (j = 0; j < arr_Tinh.Length; j++)
            {
                cb_DanToc.Items.Add(arr_Tinh[j].ToString());
            }
            Conn = prs.Connect_HRP();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select MaPB,TenPB from dbo.PhongBan";
            cmd.Connection = Conn;
            Conn.Open();
            prs.cb1(cb_PhongBan, cmd, Conn, 1, 0);
            cmd.CommandText = "select MucDo,LoaiHD from dbo.LoaiHD";
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                cb_LoaiHD_HDLD.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                cb_LoaiHD_TTHDLD.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                cb_LoaiHD_reportHDLD.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
            }
            rd.Close();
            cmd.CommandText = "select MaVung,TenVung from dbo.CMND";
            rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                txt_NoiCap_SYLL.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
            }
            rd.Close();
            cmd.CommandText = "select convert(char(10),getdate(),103)";
            string_temp = cmd.ExecuteScalar().ToString();
            Date_NgayThuViec.Text = string_temp;
            Date_NgayVaoLam.Text = string_temp;
            //date_NgaySinhTTNV.Text = string_temp;
            Conn.Close();
            cmd.Dispose();
            rd.Dispose();
        }
        #endregion
        #region //Thong Tin Nhan Vien
        private string MaNV1, Ho1, Ten1, NgaySinh1, NoiSinh1,GioiTinh1,CMND1, TDVH1, QueQuan1, ThuongTru1, TamTru1, ThanhPho1, TTHN1, DanToc1, TonGiao1, QuocTich1, ChieuCao1, CanNang1, SucKhoe1, Email1, GhiChu1, ChucVu1, PhongBan1, ToNhom1, ChuyenMon1, BenhVien1, NganHang1, SoTK1, HDLD1, NgayVaoLam1, NgayThuViec1, MucLuong1,child=" - ",NgayCap1,NoiCap1,Nhom1,Khoi1,PhuCap1;
        private string LoaiHD1, NguoiSDLD1, NgayHD1, NgayHH1, LuongPC1, PhuLuc1,MST1,SoLD1;
        private void reset_TTNV()
        {
            txt_MaNV.Text = "";
            txt_Ho.Text = "";
            txt_Ten.Text = "";
            txt_date_SYLL.Text = "";
            txt_month_SYLL.Text = "";
            txt_year_SYLL.Text = "";
            txt_NoiSinh.Text = "";
            cb_GioiTinh.Text = "";
            txt_CMND.Text = "";
            cb_TDVH.Text = "";
            cb_QueQuan.Text = "";
            txt_ThuongTru.Text = "";
            txt_TamTru.Text = "";
            cb_ThanhPho.Text = "";
            cb_TTHN.Text = "";
            cb_DanToc.Text = "";
            cb_TonGiao.Text = "";
            cb_QuocTich.Text = "";
            txt_ChieuCao.Text = "";
            txt_CanNang.Text = "";
            txt_SucKhoe.Text = "";
            txt_Email.Text = "";
            txt_MaNV.Focus();
            txt_MucLuong.Text = "";
            txt_SoTK.Text = "";
            txt_BenhVien.Text = "";
            txt_ChuyenMon.Text = "";
            txt_MST_SYLL.Text = "";
            txt_SoLD_SYLL.Text = "";
            txt_HDLD.Text = "";
            bt_DeleteTTNV.Enabled = false;
            bt_EditTTNV.Enabled = false;
            txt_MaNV.Enabled = true;
            lbImage.Text = "Image";
            bt_SaveTTNV.Enabled = true;
            pictureBox1.Image = null;
                 
        }
        private void save_image()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Conn;

            try
            {
                byte[] imageData = null;
                //Read Image Bytes into a byte array
                if (lbImage.Text != "Image")
                    imageData = ReadFile(lbImage.Text);



                //Initialize SQL Server Connection
                //SqlConnection CN = new SqlConnection(txtConnectionString.Text);

                MaNV1 = txt_MaNV.Text.Trim();
                Ho1 = txt_Ho.Text.Trim();
                Ten1 = txt_Ten.Text.Trim();
                NgaySinh1 = txt_month_SYLL.Text + "/" + txt_date_SYLL.Text + "/" + txt_year_SYLL.Text;

                NoiSinh1 = txt_NoiSinh.Text;
                GioiTinh1 = cb_GioiTinh.Text;
                CMND1 = txt_CMND.Text;
                NgayCap1 = Date_Split(txt_NgayCap_SYLL.Text);
                NoiCap1 = txt_NoiCap_SYLL.Text;
                TDVH1 = cb_TDVH.Text;
                QueQuan1 = cb_QueQuan.Text;
                ThuongTru1 = txt_ThuongTru.Text;
                TamTru1 = txt_TamTru.Text;
                ThanhPho1 = cb_ThanhPho.Text;
                TTHN1 = cb_TTHN.Text;
                DanToc1 = cb_DanToc.Text;
                TonGiao1 = cb_TonGiao.Text;
                QuocTich1 = cb_QuocTich.Text;
                ChieuCao1 = txt_ChieuCao.Text;
                Khoi1 = cb_Khoi_SYLL.Text.Trim();
                Nhom1 = txt_Nhom_SYLL.Text.Trim();
                if (txt_ChieuCao.Text != "")
                    ChieuCao1 = double.Parse(ChieuCao1).ToString();
                CanNang1 = txt_CanNang.Text;
                if (txt_CanNang.Text != "")
                    CanNang1 = double.Parse(CanNang1).ToString();
                SucKhoe1 = txt_SucKhoe.Text;
                Email1 = txt_Email.Text;
                GhiChu1 = txt_GhiChu.Text;
                PhongBan1 = prs.substring_right(cb_PhongBan.Text, child);
                ChucVu1 = cb_ChucVu.Text;
                ToNhom1 = cb_ToNhom.Text;
                ChuyenMon1 = txt_ChuyenMon.Text;
                BenhVien1 = txt_BenhVien.Text;
                NganHang1 = cb_NganHang.Text;
                if(cb_NganHang.Text!="")
                    NganHang1 = prs.substring_left(cb_NganHang.Text, child);

                SoTK1 = txt_SoTK.Text;
                MST1 = txt_MST_SYLL.Text;
                SoLD1 = txt_SoLD_SYLL.Text;
                HDLD1 = txt_HDLD.Text;

                
                NgayVaoLam1 = Ngay_Split(Date_NgayVaoLam.Text);
                
                NgayThuViec1 = Ngay_Split(Date_NgayThuViec.Text);
                MucLuong1 = txt_MucLuong.Text;
                if (txt_MucLuong.Text != "")
                    MucLuong1 = double.Parse(MucLuong1).ToString();
                if (MaNV1 == "" || Ho1 == "" || Ten1 == "" || CMND1 == "" || GioiTinh1 == ""||PhongBan1==""||txt_month_SYLL.Text==""||txt_date_SYLL.Text==""||txt_year_SYLL.Text=="")
                    MessageBox.Show("Phải nhập đầy đủ các thông tin có dấu (*)");
                else
                {
                    cmd.Parameters.AddWithValue("@MaNV", MaNV1);
                    cmd.Parameters.AddWithValue("@Ho", Ho1);
                    cmd.Parameters.AddWithValue("@Ten", Ten1);
                    cmd.Parameters.AddWithValue("@NgaySinh", NgaySinh1);
                    cmd.Parameters.AddWithValue("@NoiSinh", NoiSinh1);
                    cmd.Parameters.AddWithValue("@GioiTinh", GioiTinh1);
                    cmd.Parameters.AddWithValue("@CMND", CMND1);
                    cmd.Parameters.AddWithValue("@NgayCap", NgayCap1);
                    cmd.Parameters.AddWithValue("@NoiCap", NoiCap1);
                    cmd.Parameters.AddWithValue("@TDVH", TDVH1);
                    cmd.Parameters.AddWithValue("@QueQuan", QueQuan1);
                    cmd.Parameters.AddWithValue("@ThuongTru", ThuongTru1);
                    cmd.Parameters.AddWithValue("@TamTru", TamTru1);
                    cmd.Parameters.AddWithValue("@ThanhPho", ThanhPho1);
                    cmd.Parameters.AddWithValue("@TTHN", TTHN1);
                    cmd.Parameters.AddWithValue("@DanToc", DanToc1);
                    cmd.Parameters.AddWithValue("@TonGiao", TonGiao1);
                    cmd.Parameters.AddWithValue("@QuocTich", QuocTich1);
                    cmd.Parameters.AddWithValue("@ChieuCao", ChieuCao1);
                    cmd.Parameters.AddWithValue("@CanNang", CanNang1);
                    cmd.Parameters.AddWithValue("@SucKhoe", SucKhoe1);
                    cmd.Parameters.AddWithValue("@Email", Email1);
                    cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                    cmd.Parameters.AddWithValue("@UsersNhap", lb_User.Text);
                    cmd.Parameters.AddWithValue("@HoatDong", "1");
                    cmd.Parameters.AddWithValue("@PhongBan", PhongBan1);
                    cmd.Parameters.AddWithValue("@ChucVu", ChucVu1);
                    cmd.Parameters.AddWithValue("@ToNhom", ToNhom1);
                    cmd.Parameters.AddWithValue("@Nhom", Nhom1);
                    cmd.Parameters.AddWithValue("@Khoi", Khoi1);
                    cmd.Parameters.AddWithValue("@ChuyenMon", ChuyenMon1);
                    cmd.Parameters.AddWithValue("@BenhVien", BenhVien1);
                    cmd.Parameters.AddWithValue("@NganHang", NganHang1);
                    cmd.Parameters.AddWithValue("@SoTK", SoTK1);
                    cmd.Parameters.AddWithValue("@MST", MST1);
                    cmd.Parameters.AddWithValue("@SoLD", SoLD1);
                    cmd.Parameters.AddWithValue("@HDLD", HDLD1);
                    cmd.Parameters.AddWithValue("@NgayVaoLam", NgayVaoLam1);
                    cmd.Parameters.AddWithValue("@NgayThuViec", NgayThuViec1);
                    cmd.Parameters.AddWithValue("@MucLuong", MucLuong1);
    
                    if (imageData != null)
                    {
                        if (imageData.Length > 50000)
                            MessageBox.Show("Bạn nên giảm dung lượng ảnh lại trước khi lưu");
                        else
                        {
                            count = 1;
                            cmd.CommandText = "Ins_ThongTinNV";
                            cmd.Parameters.AddWithValue("@HinhAnh", (object)imageData);
                        }
                    }
                    else
                    {
                        count = 1;
                        cmd.CommandText = "Ins_ThongTinNV1";
                    }
                    if (count == 1)
                    { cmd.ExecuteNonQuery(); }
                    cmd.Parameters.Clear();

                    lbImage.Text = "Image";
                    pictureBox1.Image = null;
                    reset_TTNV();
                    MessageBox.Show("Dữ liệu được lưu thành công");

                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công "+ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //MessageBox.Show(ex.ToString());
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }
        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
        private void get_image(PictureBox pt, byte[] imageData)
        {
            Image newImage;
            //Read image data into a memory stream
            using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
            {
                ms.Write(imageData, 0, imageData.Length);

                //Set image variable value using memory stream.
                newImage = Image.FromStream(ms, true);
            }

            //set picture
            pt.Image = newImage;
            
        }
        private string Openfile_Image()
        {
            string path = null;
            OpenFileDialog dlg = new OpenFileDialog();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes != DialogResult.Cancel)
            {
                //Set image in picture box
                pictureBox1.ImageLocation = dlg.FileName;

                //Provide file path in txtImagePath text box.
                path= dlg.FileName;
                return path;
            }
            return null;
        }
        private void Search_TTNV()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            try
            {
                string_temp = @"SELECT     dbo.ThongTinNV.MaNV, dbo.ThongTinNV.Ho, dbo.ThongTinNV.Ten, convert(char(10),dbo.ThongTinNV.NgaySinh,103) as NgaySinh, dbo.ThongTinNV.NoiSinh, dbo.ThongTinNV.GioiTinh, 
                      dbo.ThongTinNV.CMND,convert(varchar(10),dbo.ThongTinNV.NgayCap,103) as NgayCap,dbo.ThongTinNV.NoiCap, dbo.ThongTinNV.TDVH, dbo.ThongTinNV.QueQuan, dbo.ThongTinNV.ThuongTru, dbo.ThongTinNV.TamTru, dbo.ThongTinNV.ThanhPho, 
                      dbo.ThongTinNV.TTHN, dbo.ThongTinNV.DanToc, dbo.ThongTinNV.TonGiao, dbo.ThongTinNV.QuocTich, dbo.ThongTinNV.ChieuCao, dbo.ThongTinNV.CanNang, 
                      dbo.ThongTinNV.SucKhoe, dbo.ThongTinNV.Email, dbo.ThongTinNV.HinhAnh, dbo.CongTy.ChucVu, dbo.CongTy.PhongBan, dbo.CongTy.ToNhom,dbo.CongTy.Nhom,dbo.CongTy.Khoi, 
                      dbo.CongTy.ChuyenMon, dbo.CongTy.BenhVien, dbo.CongTy.NganHang, dbo.CongTy.SoTK,dbo.CongTy.MST,dbo.CongTy.SoLD, dbo.CongTy.HDLD, convert(char(10),dbo.CongTy.NgayVaoLam,103) as NgayVaoLam, convert(char(10),dbo.CongTy.NgayThuViec,103) as NgayThuViec, 
                      dbo.CongTy.MucLuong, dbo.CongTy.Serial
                      FROM         dbo.ThongTinNV INNER JOIN
                      dbo.CongTy ON dbo.ThongTinNV.MaNV = dbo.CongTy.MaNV where HoatDong=1";
                
                if (txt_MaNV.Text != "")
                    string_temp += @" and dbo.ThongTinNV.MaNV like '%" + txt_MaNV.Text + "%'";
                if (txt_Ten.Text != "")
                    string_temp += " and Ten like '%" + txt_Ten.Text + "%'";
                if(txt_Ho.Text!="")
                    string_temp += " and Ho like '%" + txt_Ho.Text + "%'";
                if(txt_CMND.Text!="")
                    string_temp += " and CMND like '%" + txt_CMND.Text + "%'";
                if(txt_Email.Text!="")
                    string_temp += " and Email like '%" + txt_Email.Text + "%'";
                //if (cb_PhongBan.Text != "")
                    
                DataSet ds = new DataSet();
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                   
                    dataGridView1.DataSource = ds.Tables[0];
                    Display_TTNV(dataGridView1.Rows.Count - 2);
                    lb_index.Text = (dataGridView1.Rows.Count - 2).ToString();
                    txt_MaNV.Enabled = false;
                    bt_EditTTNV.Enabled = true;
                    bt_DeleteTTNV.Enabled = true;
                    bt_SaveTTNV.Enabled = false;
                   // MessageBox.Show(ds.Tables[0].Rows[0]["HinhAnh"].GetType().ToString());
                    //MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
                }
                else
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void Display_TTNV(int row)
        {
             try
            {
                //Get image data from gridview column.
                 txt_MaNV.Text = dataGridView1.Rows[row].Cells["MaNV"].Value.ToString();
                 txt_Ho.Text = dataGridView1.Rows[row].Cells["Ho"].Value.ToString();
                 txt_Ten.Text = dataGridView1.Rows[row].Cells["Ten"].Value.ToString();
                 
                 //Ngay_split = temp[1] + "/" + temp[0] + "/" + temp[2];
                 string_temp = dataGridView1.Rows[row].Cells["NgaySinh"].Value.ToString();
                 string[] temp = string_temp.Split('/');
                 txt_date_SYLL.Text = temp[0];
                 txt_month_SYLL.Text = temp[1];
                 txt_year_SYLL.Text = temp[2];
                 txt_NoiSinh.Text = dataGridView1.Rows[row].Cells["Noisinh"].Value.ToString();
                 cb_GioiTinh.Text = dataGridView1.Rows[row].Cells["GioiTinh"].Value.ToString();
                 txt_CMND.Text = dataGridView1.Rows[row].Cells["CMND"].Value.ToString();
                 cb_TDVH.Text = dataGridView1.Rows[row].Cells["TDVH"].Value.ToString();
                 cb_QueQuan.Text = dataGridView1.Rows[row].Cells["QueQuan"].Value.ToString();
                 txt_ThuongTru.Text = dataGridView1.Rows[row].Cells["ThuongTru"].Value.ToString();
                 txt_TamTru.Text = dataGridView1.Rows[row].Cells["TamTru"].Value.ToString();
                 cb_ThanhPho.Text = dataGridView1.Rows[row].Cells["ThanhPho"].Value.ToString();
                 cb_TTHN.Text = dataGridView1.Rows[row].Cells["TTHN"].Value.ToString();
                 cb_DanToc.Text = dataGridView1.Rows[row].Cells["DanToc"].Value.ToString();
                 cb_TonGiao.Text = dataGridView1.Rows[row].Cells["TonGiao"].Value.ToString();
                 cb_QuocTich.Text = dataGridView1.Rows[row].Cells["QuocTich"].Value.ToString();
                 txt_ChieuCao.Text = dataGridView1.Rows[row].Cells["ChieuCao"].Value.ToString();
                 txt_CanNang.Text = dataGridView1.Rows[row].Cells["CanNang"].Value.ToString();
                 txt_SucKhoe.Text = dataGridView1.Rows[row].Cells["SucKhoe"].Value.ToString();
                 txt_Email.Text = dataGridView1.Rows[row].Cells["Email"].Value.ToString();
                 cb_ChucVu.Text = dataGridView1.Rows[row].Cells["ChucVu"].Value.ToString();
                 cb_PhongBan.Text = dataGridView1.Rows[row].Cells["PhongBan"].Value.ToString();
                 cb_ToNhom.Text = dataGridView1.Rows[row].Cells["ToNhom"].Value.ToString();
                 txt_ChuyenMon.Text = dataGridView1.Rows[row].Cells["ChuyenMon"].Value.ToString();
                 txt_BenhVien.Text = dataGridView1.Rows[row].Cells["BenhVien"].Value.ToString();
                 cb_NganHang.Text = dataGridView1.Rows[row].Cells["NganHang"].Value.ToString();
                 txt_SoTK.Text = dataGridView1.Rows[row].Cells["SoTK"].Value.ToString();
                 txt_HDLD.Text = dataGridView1.Rows[row].Cells["HDLD"].Value.ToString();
                 Date_NgayThuViec.Text = dataGridView1.Rows[row].Cells["NgayThuViec"].Value.ToString();
                 Date_NgayVaoLam.Text = dataGridView1.Rows[row].Cells["NgayVaoLam"].Value.ToString();
                 txt_MucLuong.Text = dataGridView1.Rows[row].Cells["MucLuong"].Value.ToString();
                 cb_Khoi_SYLL.Text = dataGridView1.Rows[row].Cells["Khoi"].Value.ToString();
                 txt_Nhom_SYLL.Text = dataGridView1.Rows[row].Cells["Nhom"].Value.ToString();
                 txt_NgayCap_SYLL.Text = dataGridView1.Rows[row].Cells["NgayCap"].Value.ToString();
                 txt_NoiCap_SYLL.Text = dataGridView1.Rows[row].Cells["NoiCap"].Value.ToString();
                 txt_MST_SYLL.Text = dataGridView1.Rows[row].Cells["MST"].Value.ToString();
                 txt_SoLD_SYLL.Text = dataGridView1.Rows[row].Cells["SoLD"].Value.ToString();
                 lb_index.Text = row.ToString();
                // string[] temp = dataGridView1.Rows[row].Cells["NgayCap"].Value.ToString().Split('/');
                 //a = dataGridView1.Rows[row].Cells["Nhom"].Value.ToString();
                 //lb_Serial.Text=dataGridView1.Rows[row].Cells["Serial"].Value.ToString();
                 //MessageBox.Show("aaa");
                 //string val = dataGridView1.Rows[row].Cells["HinhAnh"].FormattedValue as string;
                 if (DBNull.Value.Equals(dataGridView1.Rows[row].Cells["HinhAnh"].Value))
                 {
                     //MessageBox.Show("aaa");
                     pictureBox1.Image = null;
                 }
                 else
                 {

                     //MessageBox.Show("bbb");
                     byte[] imageData = (byte[])dataGridView1.Rows[row].Cells["HinhAnh"].Value;
                     //MessageBox.Show(dataGridView1.Rows[row].Cells["HinhAnh"].Value.GetType().ToString());
                     //Initialize image variable
                     Image newImage;
                     //Read image data into a memory stream
                     using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                     {
                         ms.Write(imageData, 0, imageData.Length);

                         //Set image variable value using memory stream.
                         newImage = Image.FromStream(ms, true);
                     }

                     //set picture
                     pictureBox1.Image = newImage;
                 }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            lbImage.Text = Openfile_Image();
        }
        private void bt_SaveTTNV_Click(object sender, EventArgs e)
        {
            save_image();
        }
        private void bt_Search_Click(object sender, EventArgs e)
        {
            Search_TTNV();
        }
        private void bt_prev_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_TTNV(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_next_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView1.Rows.Count - 2)
                    {
                        i++;
                        Display_TTNV(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_first_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                    Display_TTNV(0);
                    lb_index.Text = "0";
            }
        }
        private void bt_last_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                Display_TTNV(dataGridView1.Rows.Count-2);
                lb_index.Text = (dataGridView1.Rows.Count - 2).ToString();
            }
        }
        private void bt_ResetTTNV_Click(object sender, EventArgs e)
        {
            reset_TTNV();
        }
        private void Edit_TTNV()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Conn;

            try
            {
                byte[] imageData = null;
                //Read Image Bytes into a byte array
                if (lbImage.Text != "Image")
                    imageData = ReadFile(lbImage.Text);



                //Initialize SQL Server Connection
                //SqlConnection CN = new SqlConnection(txtConnectionString.Text);

                MaNV1 = txt_MaNV.Text.Trim();
                Ho1 = txt_Ho.Text.Trim();
                Ten1 = txt_Ten.Text.Trim();
                NgaySinh1 = txt_month_SYLL.Text + "/" + txt_date_SYLL.Text + "/" + txt_year_SYLL.Text;
                
                NoiSinh1 = txt_NoiSinh.Text;
                GioiTinh1 = cb_GioiTinh.Text;
                CMND1 = txt_CMND.Text;
                NgayCap1 = Date_Split(txt_NgayCap_SYLL.Text);
                MessageBox.Show(NgaySinh1+" "+NgayCap1);
                NoiCap1 = txt_NoiCap_SYLL.Text;
                TDVH1 = cb_TDVH.Text;
                QueQuan1 = cb_QueQuan.Text;
                ThuongTru1 = txt_ThuongTru.Text;
                TamTru1 = txt_TamTru.Text;
                ThanhPho1 = cb_ThanhPho.Text;
                TTHN1 = cb_TTHN.Text;
                DanToc1 = cb_DanToc.Text;
                TonGiao1 = cb_TonGiao.Text;
                QuocTich1 = cb_QuocTich.Text;
                ChieuCao1 = txt_ChieuCao.Text;
                if (txt_ChieuCao.Text != "")
                    ChieuCao1 = double.Parse(ChieuCao1).ToString();
                CanNang1 = txt_CanNang.Text;
                if (txt_CanNang.Text != "")
                    CanNang1 = double.Parse(CanNang1).ToString();
                SucKhoe1 = txt_SucKhoe.Text;
                Email1 = txt_Email.Text;
                GhiChu1 = txt_GhiChu.Text;
                if (cb_PhongBan.Text != "")
                {
                    PhongBan1 = prs.substring_right(cb_PhongBan.Text, child);
                    if (PhongBan1 == "")
                        PhongBan1 = cb_PhongBan.Text;
                }
                ChucVu1 = cb_ChucVu.Text;
                ToNhom1 = cb_ToNhom.Text;
                Nhom1 = txt_Nhom_SYLL.Text.Trim();
                Khoi1 = cb_Khoi_SYLL.Text.Trim();
                ChuyenMon1 = txt_ChuyenMon.Text;
                BenhVien1 = txt_BenhVien.Text;
                NganHang1 = cb_NganHang.Text;
                if (cb_NganHang.Text != "")
                {
                    NganHang1 = prs.substring_left(cb_NganHang.Text, child);
                    if (NganHang1 == "")
                        NganHang1 = cb_NganHang.Text;
                }
                SoTK1 = txt_SoTK.Text;
                MST1 = txt_MST_SYLL.Text;
                SoLD1 = txt_SoLD_SYLL.Text;
                HDLD1 = txt_HDLD.Text;


                NgayVaoLam1 = Ngay_Split(Date_NgayVaoLam.Text);

                NgayThuViec1 = Ngay_Split(Date_NgayThuViec.Text);
                MucLuong1 = txt_MucLuong.Text;
                if (txt_MucLuong.Text != "")
                    MucLuong1 = double.Parse(MucLuong1).ToString();
                if (MaNV1 == "" || Ho1 == "" || Ten1 == "" || CMND1 == "" || GioiTinh1 == ""||PhongBan1==""||txt_month_SYLL.Text==""||txt_date_SYLL.Text==""||txt_year_SYLL.Text=="")
                    MessageBox.Show("Phải nhập đầy đủ các thông tin có dấu (*)");
                else
                {
                    DialogResult TL;
                    TL = MessageBox.Show("THÔNG TIN CÓ MÃ NHÂN VIÊN: "+txt_MaNV.Text+" SẼ ĐƯỢC SỬA?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (TL == DialogResult.Yes)
                    {

                        cmd.Parameters.AddWithValue("@MaNV", MaNV1);
                        cmd.Parameters.AddWithValue("@Ho", Ho1);
                        cmd.Parameters.AddWithValue("@Ten", Ten1);
                        cmd.Parameters.AddWithValue("@NgaySinh", NgaySinh1);
                        cmd.Parameters.AddWithValue("@NoiSinh", NoiSinh1);
                        cmd.Parameters.AddWithValue("@GioiTinh", GioiTinh1);
                        cmd.Parameters.AddWithValue("@CMND", CMND1);
                       
                        cmd.Parameters.AddWithValue("@NgayCap", NgayCap1);
                        
                        cmd.Parameters.AddWithValue("@NoiCap", NoiCap1);
                        cmd.Parameters.AddWithValue("@TDVH", TDVH1);
                        cmd.Parameters.AddWithValue("@QueQuan", QueQuan1);
                        cmd.Parameters.AddWithValue("@ThuongTru", ThuongTru1);
                        cmd.Parameters.AddWithValue("@TamTru", TamTru1);
                        cmd.Parameters.AddWithValue("@ThanhPho", ThanhPho1);
                        cmd.Parameters.AddWithValue("@TTHN", TTHN1);
                        cmd.Parameters.AddWithValue("@DanToc", DanToc1);
                        cmd.Parameters.AddWithValue("@TonGiao", TonGiao1);
                        cmd.Parameters.AddWithValue("@QuocTich", QuocTich1);
                        cmd.Parameters.AddWithValue("@ChieuCao", ChieuCao1);
                        cmd.Parameters.AddWithValue("@CanNang", CanNang1);
                        cmd.Parameters.AddWithValue("@SucKhoe", SucKhoe1);
                        cmd.Parameters.AddWithValue("@Email", Email1);
                        cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                        //cmd.Parameters.AddWithValue("@UsersNhap", lb_User.Text);
                        // cmd.Parameters.AddWithValue("@HoatDong", "1");
                        cmd.Parameters.AddWithValue("@PhongBan", PhongBan1);
                        cmd.Parameters.AddWithValue("@ChucVu", ChucVu1);
                        cmd.Parameters.AddWithValue("@ToNhom", ToNhom1);
                        cmd.Parameters.AddWithValue("@Nhom", Nhom1);
                        cmd.Parameters.AddWithValue("@Khoi", Khoi1);
                        cmd.Parameters.AddWithValue("@ChuyenMon", ChuyenMon1);
                        cmd.Parameters.AddWithValue("@BenhVien", BenhVien1);
                        cmd.Parameters.AddWithValue("@NganHang", NganHang1);
                        cmd.Parameters.AddWithValue("@SoTK", SoTK1);
                        cmd.Parameters.AddWithValue("@MST", MST1);
                        cmd.Parameters.AddWithValue("@SoLD", SoLD1);
                        cmd.Parameters.AddWithValue("@HDLD", HDLD1);
                        cmd.Parameters.AddWithValue("@NgayVaoLam", NgayVaoLam1);
                        cmd.Parameters.AddWithValue("@NgayThuViec", NgayThuViec1);
                        cmd.Parameters.AddWithValue("@MucLuong", MucLuong1);

                        if (imageData != null)
                        {
                            if (imageData.Length > 50000)
                                MessageBox.Show("Bạn nên giảm dung lượng ảnh lại trước khi lưu");
                            else
                            {
                                count = 1;
                                cmd.CommandText = "Upd_ThongTinNV";
                                cmd.Parameters.AddWithValue("@HinhAnh", (object)imageData);
                            }
                        }
                        else
                        {
                            count = 1;
                            cmd.CommandText = "Upd_ThongTinNV1";
                        }
                        if (count == 1)
                        { cmd.ExecuteNonQuery(); }
                        cmd.Parameters.Clear();

                        lbImage.Text = "Image";
                        pictureBox1.Image = null;
                        reset_TTNV();
                        MessageBox.Show("Dữ liệu được sửa thành công");
                    }
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err=MessageBox.Show("KIỂM TRA LẠI THÔNG TIN NHẬP, SỬA KHÔNG THÀNH CÔNG "+ex.ToString(),"THÔNG BÁO",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }
        private void bt_EditTTNV_Click(object sender, EventArgs e)
        {
            Edit_TTNV();
        }
        private void delete()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            DialogResult TL;
            try
            {
                TL = MessageBox.Show("THÔNG TIN CÓ MÃ NHÂN VIÊN: " + txt_MaNV.Text + " SẼ BỊ XÓA?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (TL == DialogResult.Yes)
                {
                    cmd.CommandText = "UPDATE [HRP_ScansiaPacific].[dbo].[ThongTinNV] SET [HoatDong] = 0 where MaNV='" + txt_MaNV.Text + "'";
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("ĐÃ XẢY RA LỖI KHI XÓA DỮ LIỆU " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }
        private void txt_ChieuCao_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_ChieuCao);
        }

        private void txt_CanNang_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_CanNang);
        }

        private void txt_MucLuong_TextChanged(object sender, EventArgs e)
        {
            checktext_number(txt_MucLuong);
        }
        private void txt_date_SYLL_Leave(object sender, EventArgs e)
        {
            if (txt_date_SYLL.Text != "")
            {
                try
                {
                    int_temp = int.Parse(txt_date_SYLL.Text);
                    if (int_temp > 31)
                    {
                        MessageBox.Show("Giá trị nhập không hợp lệ 1<= Date <=31");
                        txt_date_SYLL.Text = "";
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void txt_month_SYLL_Leave(object sender, EventArgs e)
        {
            if (txt_month_SYLL.Text != "")
            {
                int_temp = int.Parse(txt_month_SYLL.Text);
                if (int_temp > 12)
                {
                    MessageBox.Show("Giá trị nhập không hợp lệ 1<= Month <=12");
                    txt_month_SYLL.Text = "";
                }
            }
        }

        private void txt_year_SYLL_Leave(object sender, EventArgs e)
        {
            if (txt_year_SYLL.Text != "")
            {
                try
                {
                    int_temp = int.Parse(txt_year_SYLL.Text);
                    if (int_temp < 1900)
                    {
                        MessageBox.Show("Giá trị nhập không hợp lệ, năm phải gồm 4 chữ số");
                        txt_year_SYLL.Text = "";
                    }
                    if (txt_month_SYLL.Text != "")
                    {
                        i = int.Parse(txt_month_SYLL.Text);
                        if (txt_date_SYLL.Text != "")
                        {
                            j = int.Parse(txt_date_SYLL.Text);
                            if (i == 2)
                            {
                                if (j >= 30)
                                {
                                    MessageBox.Show("Giá trị không vượt giá 29");
                                    txt_date_SYLL.Text = "";
                                }
                                else
                                {
                                    if (int_temp % 4 != 0)
                                    {
                                        if (j == 29)
                                        {
                                            MessageBox.Show("Năm " + int_temp.ToString() + " tháng 2 chỉ có 28 ngày");
                                            txt_date_SYLL.Text = "";
                                        }
                                    }
                                }

                            }
                            if (i == 4 || i == 6 || i == 9 || i == 11)
                            {
                                if (j == 31)
                                {
                                    MessageBox.Show("Tháng " + i.ToString() + " chỉ có 30 ngày");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void txt_CMND_Leave(object sender, EventArgs e)
        {
            if (txt_CMND.Text != "")
            {
                if (txt_CMND.Text.Length < 10)
                {
                    string_temp = txt_CMND.Text.Substring(0, 2);
                    for (i = 0; i < txt_NoiCap_SYLL.Items.Count; i++)
                    {
                        if (string_temp == prs.substring_right(txt_NoiCap_SYLL.Items[i].Text, child))
                        {
                            txt_NoiCap_SYLL.Text = txt_NoiCap_SYLL.Items[i].Text;
                            break;
                        }


                    }
                }
                if (i == txt_NoiCap_SYLL.Items.Count||txt_CMND.Text.Length>=10)
                    MessageBox.Show("Kiểm tra lại số chứng minh nhân dân");
            }
        }
        #endregion


        #region//Tim Kiem Nhan Vien

        private void TTNV_Search()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            try
            {
                string_temp = @"SELECT     dbo.ThongTinNV.MaNV, dbo.ThongTinNV.Ho+' '+dbo.ThongTinNV.Ten as HovaTen, convert(char(10),dbo.ThongTinNV.NgaySinh,103) as NgaySinh,dbo.ThongTinNV.GioiTinh, 
                      dbo.ThongTinNV.CMND, 
                      
                      dbo.ThongTinNV.HinhAnh, dbo.CongTy.ChucVu, dbo.CongTy.PhongBan,
                      convert(char(10),dbo.CongTy.NgayVaoLam,103) as NgayVaoLam,dbo.CongTy.ChuyenMon,
                      N'Họ và tên: '+Ho+' '+Ten+char(10)+N'Giới tính: '+GioiTinh+char(10)+N'Bộ phận: '+dbo.CongTy.PhongBan+char(10)+N'Ngày vào làm:'+convert(char(10),dbo.CongTy.NgayVaoLam,103)+char(10)+N'Năm sinh: '+convert(char(10),dbo.ThongTinNV.NgaySinh,103)+char(10)+N'Chuyên môn: '+ChuyenMon as ThongTin
                      FROM         dbo.ThongTinNV INNER JOIN
                      dbo.CongTy ON dbo.ThongTinNV.MaNV = dbo.CongTy.MaNV where HoatDong=1";

                if (txt_MaNV_Search.Text != "")
                    string_temp += @" and dbo.ThongTinNV.MaNV like '%" + txt_MaNV_Search.Text + "%'";
                if (txt_TenNV_Search.Text != "")
                    string_temp += " and Ten like '%" + txt_TenNV_Search.Text + "%'";

                if (txt_CMND_Search.Text != "")
                    string_temp += " and CMND like '%" + txt_CMND_Search.Text + "%'";
                if (cb_PhongBan_Search.Text != "")
                    string_temp += " and PhongBan like '%" + prs.substring_right(cb_PhongBan_Search.Text, child)+"%'";
                if (txt_ChuyenMon_Search.Text != "")
                    string_temp += " and ChuyenMon like '%" + txt_ChuyenMon_Search.Text + "%'";
                if(txt_NgayVaoLam_Search.Text!="")
                {
                    NgayVaoLam1 = Ngay_Split(txt_NgayVaoLam_Search.Text);
                    string_temp += " and NgayVaoLam like '%" + NgayVaoLam1 + "%'";
                }
                if (txt_NamSinh_Search.Text != "")
                    string_temp += " and datepart(yyyy,NgaySinh)=" + txt_NamSinh_Search.Text;
                //if (cb_PhongBan.Text != "")

                DataSet ds = new DataSet();
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    /*if (Gridview_Search_TTNV.Rows.Count > 0)
                        Gridview_Search_TTNV.Rows.Clear();
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        MaNV1 = ds.Tables[0].Rows[i]["MaNV"].ToString();
                        Ten1 = ds.Tables[0].Rows[i]["HovaTen"].ToString();
                        ChucVu1 = ds.Tables[0].Rows[i]["ChucVu"].ToString();
                        PhongBan1 = ds.Tables[0].Rows[i]["PhongBan"].ToString();
                        GioiTinh1 = ds.Tables[0].Rows[i]["GioiTinh"].ToString();
                        NgaySinh1 = ds.Tables[0].Rows[i]["NgaySinh"].ToString();
                        CMND1 = ds.Tables[0].Rows[i]["CMND"].ToString();
                        NgayVaoLam1 = ds.Tables[0].Rows[i]["NgayVaoLam"].ToString();
                        GhiChu1 = ds.Tables[0].Rows[i]["ThongTin"].ToString();
                        Gridview_Search_TTNV.Rows.Add(MaNV1, Ten1, ChucVu1, GioiTinh1, PhongBan1, NgaySinh1, CMND1, NgayVaoLam1, ds.Tables[0].Rows[i]["HinhAnh"],GhiChu1);
                        
                    }
                    ds.Clear();*/
                    Gridview_Search_TTNV.DataSource = ds.Tables[0];
                    this.Gridview_Search_TTNV.ReadOnly = true;
                    this.Gridview_Search_TTNV.AllowColumnReorder = false;
                    this.Gridview_Search_TTNV.AllowColumnResize = true;
                    //this.Gridview_Search_TTNV.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                    this.Gridview_Search_TTNV.AutoExpandGroups = false;
                    this.Gridview_Search_TTNV.TableElement.RowHeight = 20;
                    //this.Gridview_Search_TTNV.GroupDescriptors.Add(new GridGroupByExpression("Phong Ban GroupBy PhongBan_Search"));
                    //MessageBox.Show("aaaa");

                    //this.Gridview_Search_TTNV.Groups[1][0].IsCurrent = true;
                    this.Gridview_Search_TTNV.Columns["Image_search"].VisibleInColumnChooser = false;
                    //MessageBox.Show("bbbb");
                    this.Gridview_Search_TTNV.Columns["Image_search"].IsVisible = false;
                    //this.Gridview_Search_TTNV.Columns["ThongTin_search"].WrapText = true;
                    //this.Gridview_Search_TTNV.Columns["LastName"].SortOrder = Telerik.WinControls.UI.RadSortOrder.Descending;
                    // MessageBox.Show("cccc");
                    this.Gridview_Search_TTNV.DetailsColumn = this.Gridview_Search_TTNV.Columns["NgaySinh_search"];
                    //MessageBox.Show("asd");
                    //MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
                }
                else
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void bt_Tim_Search_Click(object sender, EventArgs e)
        {
            TTNV_Search();
        }
        private void Display_SearchTTNV(int r)
        {
            try
            {
            lb_MaNV.Text = Gridview_Search_TTNV.Rows[r].Cells["MaNV_Search"].Value.ToString();
            lb_HovaTen.Text = Gridview_Search_TTNV.Rows[r].Cells["HoTen_Search"].Value.ToString();
            lb_ChucVu.Text = Gridview_Search_TTNV.Rows[r].Cells["ChucVu_Search"].Value.ToString();
            lb_BoPhan.Text = Gridview_Search_TTNV.Rows[r].Cells["PhongBan_Search"].Value.ToString();
            lb_CMND.Text = Gridview_Search_TTNV.Rows[r].Cells["CMND_Search"].Value.ToString();
            lb_NgayVaoLam.Text = Gridview_Search_TTNV.Rows[r].Cells["NgayVaoLam_Search"].Value.ToString();
            lb_Phai.Text = Gridview_Search_TTNV.Rows[r].Cells["Phai_Search"].Value.ToString();
            lb_Tuoi.Text = Gridview_Search_TTNV.Rows[r].Cells["NgaySinh_Search"].Value.ToString();
            lb_index.Text = r.ToString();
            if (DBNull.Value.Equals(Gridview_Search_TTNV.Rows[r].Cells["Image_search"].Value))
                 {
                     //MessageBox.Show("aaa");
                     pictureBox3.Image = null;
                 }
                 else
                 {
                     //MessageBox.Show("aaaa");
                     //byte[] imageData = (byte[])Gridview_Search_TTNV.Rows[r].Cells["Image_search"].Value;
                     MemoryStream memoryStream = new MemoryStream();
                     Bitmap newBitmap = (Bitmap)Gridview_Search_TTNV.Rows[r].Cells["Image_search"].Value;
                     if (newBitmap != null)
                     {
                         newBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);

                         byte[] imageData = memoryStream.ToArray();
                         //Initialize image variable
                         Image newImage;
                         //Read image data into a memory stream
                         using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                         {
                             ms.Write(imageData, 0, imageData.Length);

                             //Set image variable value using memory stream.
                             newImage = Image.FromStream(ms, true);
                         }
                         //MessageBox.Show("dddd");
                         //set picture
                         pictureBox3.Image = newImage;
                     }
                     else
                         pictureBox3.Image = null;
                 }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
        private void radLabel1_DoubleClick(object sender, EventArgs e)
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                cmd.CommandText = @"select DATEPART(YYYY,getdate())-2000,DATEPART(MM,GETDATE()),convert(int,right(MaNV,3))+1,DateDiff(MM,Ngay,getdate()) from dbo.ThongTinNV
                    where Serial=(select MAX(Serial) from dbo.ThongTinNV)";
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    if (int.Parse(rd[3].ToString()) == 1)
                    {
                        string_temp = "001";
                    }
                    if (int.Parse(rd[3].ToString()) == 0)
                    {
                        i = rd[2].ToString().Length;
                        if (i == 1)
                            string_temp = "00" + rd[2].ToString();
                        if (i == 2)
                            string_temp = "0" + rd[2].ToString();
                        if (i >= 3)
                            string_temp = rd[2].ToString();
                    }

                    if (rd[1].ToString().Length < 2)
                        MaNV1 = rd[0].ToString() + "0" + rd[1].ToString() + string_temp;
                    else
                        MaNV1 = rd[0].ToString() + rd[1].ToString() + string_temp;
                }
                txt_MaNV.Text = MaNV1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        #endregion
        #region //Hop Dong LD
       

        private void cb_MaNV_NLD_Leave(object sender, EventArgs e)
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            try
            {
                string_temp = @"SELECT     dbo.ThongTinNV.MaNV,Ho+' '+Ten as HovaTen,HinhAnh from  dbo.ThongTinNV where MaNV='"+cb_MaNV_NLD.Text+"'";
                DataSet ds = new DataSet();
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    txt_HoTen_NLD.Text = ds.Tables[0].Rows[0]["HovaTen"].ToString();
                    if (DBNull.Value.Equals(ds.Tables[0].Rows[0]["HinhAnh"]))
                    {
                        //MessageBox.Show("aaa");
                        pictureBox2.Image = null;
                    }
                    else
                         get_image(pictureBox2, (byte[])ds.Tables[0].Rows[0]["HinhAnh"]);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();

            }
        }
        /*private void save_HDLD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Conn;
            MucLuong1 = "0";
            LuongPC1 = "0";
            try
            {
                MaNV1 = cb_MaNV_NLD.Text.Trim();
                LoaiHD1 = cb_LoaiHD_NLD.Text.Trim();
                NguoiSDLD1 = txt_NguoiDaiDien_HD.Text.Trim();
                NgayHD1 = Ngay_Split(Date_TuNgay_NLD.Text);
                NgayHH1 = Ngay_Split(Date_DenNgay_NLD.Text);
                if(txt_MucLuong_NLD.Text!="")
                    MucLuong1 = double.Parse(txt_MucLuong_NLD.Text).ToString();
                if(txt_PhuCap_NLD.Text!="")
                    LuongPC1 = double.Parse(txt_PhuCap_NLD.Text).ToString();
                PhuLuc1 = txt_PhuLuc_NLD.Text;
                if (MaNV1 == "" || txt_MucLuong_NLD.Text == "" || NgayHD1 == "" || NgayHH1 =="")
                    MessageBox.Show("Phải nhập đầy đủ các thông tin có dấu (*)");
                else
                {
                    cmd.CommandText = "Ins_HopDong";
                    cmd.Parameters.AddWithValue("@MaNV", MaNV1);
                    cmd.Parameters.AddWithValue("@LoaiHD", LoaiHD1);
                    cmd.Parameters.AddWithValue("@NguoiSDLD", NguoiSDLD1);
                    cmd.Parameters.AddWithValue("@NgayHD", NgayHD1);
                    cmd.Parameters.AddWithValue("@NgayHH", NgayHH1);
                    cmd.Parameters.AddWithValue("@Luong", MucLuong1);
                    cmd.Parameters.AddWithValue("@LuongPC", LuongPC1);
                    cmd.Parameters.AddWithValue("@PhuLuc", PhuLuc1);
                    cmd.Parameters.AddWithValue("@UserNhap", lb_User.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    cb_MaNV_NLD.Text = "";
                    txt_MucLuong_NLD.Text = "";
                    txt_PhuCap_NLD.Text = "";
                    txt_MucLuong_NLD.Text = "";
                    txt_PhuLuc_NLD.Text = "";
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //MessageBox.Show(ex.ToString());
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }*/
        private void Gridview_Search_TTNV_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.RowInfo.IsCurrent && e.CellElement.ColumnInfo == this.Gridview_Search_TTNV.DetailsColumn)
            {
                e.CellElement.DrawFill = true;
                e.CellElement.GradientStyle = GradientStyles.Solid;
                e.CellElement.BackColor = Color.White;
                e.CellElement.TextImageRelation = TextImageRelation.ImageBeforeText;
                e.CellElement.TextAlignment = ContentAlignment.TopLeft;
                e.CellElement.ImageAlignment = ContentAlignment.MiddleLeft;
                e.CellElement.Padding = new Padding(10);
                e.CellElement.ImageLayout = ImageLayout.Zoom;
                //MemoryStream memoryStream = new MemoryStream();
                //Bitmap newBitmap = (Bitmap)e.CellElement.RowInfo.Cells["Image_search"].Value;
                //if (newBitmap != null)

                // newBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);

                //byte[] imageData = memoryStream.ToArray();


                if (!DBNull.Value.Equals(e.CellElement.RowInfo.Cells["Image_search"].Value))
                {
                    byte[] bytes = (byte[])e.CellElement.RowInfo.Cells["Image_search"].Value;
                    e.CellElement.Image = GetImageFromBytes(bytes);
                }
                //MessageBox.Show("aasadsa");

            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.PaddingProperty, ValueResetFlags.Local);
            }
        }
        private const int OleHeaderSize = 78;
        private Image GetImageFromBytes(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            { return null; }
            Image result = null;
            MemoryStream stream = null;
            try
            {
                int count;
                int start;
                if (IsOleContainer(bytes))
                {
                    count = bytes.Length - OleHeaderSize;
                    start = OleHeaderSize;
                }
                else
                {
                    count = bytes.Length;
                    start = 0;
                }
                stream = new MemoryStream(bytes, start, count);
                result = Image.FromStream(stream);
            }
            catch
            { result = null; }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return result;
        }
        private bool IsOleContainer(byte[] imageData)
        {
            return (imageData != null && imageData[0] == 0x15 && imageData[1] == 0x1C);
        }

        private void Gridview_Search_TTNV_Click(object sender, EventArgs e)
        {

        }
        private void bt_save_HDLDNLD_Click(object sender, EventArgs e)
        {
            save_HDLD();
        }



        private void bt_save_HDLDTTLD_Click(object sender, EventArgs e)
        {

        }

        private void bt_Load_HDLD_Click(object sender, EventArgs e)
        {

        }

        private void cb_LoaiHD_HDLD_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            string_temp=prs.substring_left(cb_LoaiHD_HDLD.Text, child);
            if (string_temp == "Hoc viec")
            {
               // txt_Luong_HDLD.Text = "2350000";
                txt_ThoiGian_HDLD.Text = "1";
            }
            if (string_temp == "Thu viec"||string_temp=="Thoi vu")
            {
                //txt_Luong_HDLD.Text = "2990000";
                txt_ThoiGian_HDLD.Text = "1";
            }
            if (string_temp == "Mot nam" || string_temp == "Nua nam")
            {
                //txt_Luong_HDLD.Text = "3000000";
                txt_ThoiGian_HDLD.Text = "12";

            }
            if (string_temp == "Ba nam")
            {
                //txt_Luong_HDLD.Text = "5000000";
                txt_ThoiGian_HDLD.Text = "36";

            }
            if (string_temp == "Khong thoi han")
            {
                //txt_Luong_HDLD.Text = "5000000";
                txt_ThoiGian_HDLD.Text = "";
            }
        }
        private void Load_Data_HDLD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            try
            {
                if (radGridView1.Rows.Count > 0)
                    radGridView1.Rows.Clear();
                if (txt_Luong_HDLD.Text != "" && cb_LoaiHD_HDLD.Text != "")
                {
                    int_temp = int.Parse(prs.substring_right(cb_LoaiHD_HDLD.Text, child));
                    if (int_temp == 0)
                        int_temp = int_temp + 1;
                    if (txt_ThoiGian_HDLD.Text != "")
                    {

                        string_temp = @"SELECT     dbo.ThongTinNV.MaNV,dbo.ThongTinNV.Ho+' '+dbo.ThongTinNV.Ten as HoTen,isnull(qry1.MucDo,0) as MucDo,convert(varchar(10),isnull(qry1.NgayHH,getdate()),103) as DateBegin,convert(varchar(10),dateadd(MM," + txt_ThoiGian_HDLD.Text + ",isnull(qry1.NgayHH,getdate())),103) as DateEnd,CongTy.ChucVu,CongTy.ChuyenMon,convert(varchar(10),CongTy.NgayVaoLam,103) as NgayVaoLam,datediff(dd,getdate(),isnull(qry1.NgayHH,getdate())) as NgayCL,datepart(YY,isnull(qry1.NgayHH,getdate()))-2000 as Nam ";
                            string_temp += @" FROM         dbo.ThongTinNV left join (select HDLD.MaNV,max(HDLD.NgayHH) as NgayHH,max(LoaiHD.MucDo) as MucDo 
                                        from dbo.LoaiHD inner join dbo.HDLD on HDLD.LoaiHD=LoaiHD.LoaiHD group by MaNV) as qry1 on qry1.MaNV=dbo.ThongTinNV.MaNV inner join dbo.CongTy on dbo.CongTy.MaNV=dbo.ThongTinNV.MaNV";

                            string_temp += @" where datediff(dd,getdate(),isnull(qry1.NgayHH,getdate()))<32 and isnull(qry1.MucDo,0)=" + (int_temp - 1);
                       
                    }
                    else
                    {
                        if (int_temp == 3)
                        {
                            string_temp = @"SELECT     dbo.ThongTinNV.MaNV,dbo.ThongTinNV.Ho+' '+dbo.ThongTinNV.Ten as HoTen,isnull(qry1.MucDo,0) as MucDo,convert(varchar(10),qry1.NgayHH,103) as DateBegin,CongTy.ChucVu,CongTy.ChuyenMon,convert(varchar(10),CongTy.NgayVaoLam,103) as NgayVaoLam,'' as DateEnd,datediff(dd,getdate(),qry1.NgayHH) as NgayCL,datepart(YYYY,NgayHH)-2000 as Nam";
                            string_temp += @" FROM         dbo.ThongTinNV left join (select HDLD.MaNV,max(HDLD.NgayHH) as NgayHH,max(LoaiHD.MucDo) as MucDo 
                                        from dbo.LoaiHD inner join dbo.HDLD on HDLD.LoaiHD=LoaiHD.LoaiHD group by MaNV) as qry1 on qry1.MaNV=dbo.ThongTinNV.MaNV inner join dbo.CongTy on dbo.CongTy.MaNV=dbo.ThongTinNV.MaNV";
                            //int_temp = int.Parse(prs.substring_right(cb_LoaiHD_HDLD.Text, child));
                            string_temp += @" where datediff(dd,getdate(),qry1.NgayHH)<32 and qry1.MucDo=" + (int_temp - 1);
                        }
                    }
                    //MessageBox.Show(string_temp);
                    ds = prs.dts(string_temp, Conn, ds);
                    LoaiHD1 = prs.substring_left(cb_LoaiHD_HDLD.Text, child);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        lb_Kien.Text = "0";
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            
                            MaNV1 = ds.Tables[0].Rows[i]["MaNV"].ToString();
                            Ho1 = ds.Tables[0].Rows[i]["HoTen"].ToString();
                            ChucVu1=ds.Tables[0].Rows[i]["ChucVu"].ToString();
                            NgayVaoLam1 = ds.Tables[0].Rows[i]["NgayVaoLam"].ToString();
                            NgayHD1 = ds.Tables[0].Rows[i]["DateBegin"].ToString();
                            NgayHH1 = ds.Tables[0].Rows[i]["DateEnd"].ToString();
                            NgayCap1 = ds.Tables[0].Rows[i]["NgayCL"].ToString();
                            GhiChu1 = MaNV1 + "/HĐLĐ/SP/0" +prs.substring_right(cb_LoaiHD_HDLD.Text,child) + ds.Tables[0].Rows[i]["Nam"].ToString();
                            if(cb_BacLuong_HDLD.Text!="")
                                BacLuong1=prs.substring_left(cb_BacLuong_HDLD.Text,child);
                            radGridView1.Rows.Add(false, MaNV1, Ho1,ChucVu1, NgayVaoLam1, NgayHD1, NgayHH1, txt_Luong_HDLD.Text, LoaiHD1, NgayCap1,GhiChu1,BacLuong1);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Không có dữ liệu. Vui lòng kiểm tra lại thông tin tìm kiếm");
                    }
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                ds.Clear();
            }
        }
        private void bt_LoadData_HDLD_Click(object sender, EventArgs e)
        {
            Load_Data_HDLD();
        }
        private void radGridView1_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                radGridView1.EndEdit();
                radGridView1.CurrentRow.Cells["Check_HDLD"].BeginEdit();
               // radGridView1.IsDisplayed= radGridView1.Rows[0].;
                int r = radGridView1.CurrentCell.RowIndex;
                bool isChecked = (bool)radGridView1.CurrentRow.Cells["Check_HDLD"].Value;

                if (isChecked == true)
                {
                   
                    //MessageBox.Show(radGridView1.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());
                    count = int.Parse(lb_Kien.Text) + 1;
                    lb_Kien.Text = count.ToString();
                   
                }
                else
                {

                    //MessageBox.Show("-" + radGridView1.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());

                    count = int.Parse(lb_Kien.Text) - 1;
                    lb_Kien.Text = count.ToString();
                   
                    //MessageBox.Show("aaa");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Chk_all_HDLD_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            
            //lb_Kien.Text = "0";
            if (radGridView1.Rows.Count > 0)
            {
                radGridView1.CurrentRow = radGridView1.Rows[0];
                radGridView1.GridElement.ScrollToRow(0);
                if (Chk_all_HDLD.Checked == true)
                {
                    
                    //lb_Kien.Text = "0";
                    for (i = 0; i < radGridView1.Rows.Count; i++)
                    {
                        //if ((bool)radGridView1.Rows[i].Cells[0].Value == false)
                            radGridView1.Rows[i].Cells[0].Value = true;
                        //count++;
                    }

                }
                if (Chk_all_HDLD.Checked == false)
                {
                   
                    for (i = 0; i < radGridView1.Rows.Count; i++)
                    {

                        radGridView1.Rows[i].Cells[0].Value = false;

                    }
                   lb_Kien.Text = "0";
                }
            }
        }
        private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            temp_double = double.Parse(e.RowElement.RowInfo.Cells["SoNgayCL_HDLD"].Value.ToString());
            if (temp_double <= 5)
            {
                e.RowElement.DrawFill = true;
                e.RowElement.BackColor = System.Drawing.Color.Red;

            }
            else
            {
                if (temp_double <= 15)
                {
                    e.RowElement.DrawFill = true;
                    e.RowElement.BackColor = System.Drawing.Color.Pink;
                }
                else
                {
                    if (temp_double <= 32)
                    {
                        e.RowElement.DrawFill = true;
                        e.RowElement.BackColor = System.Drawing.Color.AliceBlue;
                    }
                }

            }

        }
        private void bt_XacNhan_HDLD_Click(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                if (radGridView2.Rows.Count > 0)
                    radGridView2.Rows.Clear();
                for (i = 0; i < radGridView1.Rows.Count; i++)
                {
                    if ((bool)radGridView1.Rows[i].Cells[0].Value == true)
                    {
                        MaNV1 = radGridView1.Rows[i].Cells["MaNV_HDLD"].Value.ToString();
                        Ho1 = radGridView1.Rows[i].Cells["HovaTen_HDLD"].Value.ToString();
                        ChucVu1 = radGridView1.Rows[i].Cells["ChucVu_HDLD"].Value.ToString();
                        NgayVaoLam1 = radGridView1.Rows[i].Cells["NgayVao_HDLD"].Value.ToString();
                        NgayHD1 = radGridView1.Rows[i].Cells["DateBegin_HDLD"].Value.ToString();
                        NgayHH1 = radGridView1.Rows[i].Cells["DateEnd_HDLD"].Value.ToString();
                        MucLuong1 = radGridView1.Rows[i].Cells["Luong_HDLD"].Value.ToString();
                        LoaiHD1 = radGridView1.Rows[i].Cells["LoaiHD_HDLD"].Value.ToString();
                        NgaySinh1 = radGridView1.Rows[i].Cells["SoNgayCL_HDLD"].Value.ToString();
                        GhiChu1 = radGridView1.Rows[i].Cells["SoHD_HDLD"].Value.ToString();

                        radGridView2.Rows.Add(true, MaNV1, Ho1, ChucVu1, NgayVaoLam1, NgayHD1, NgayHH1, MucLuong1, LoaiHD1, GhiChu1,"0","");
                        radPageView3.SelectedPage = Page_HopDong;
                    }
                }
            }
        }
        private void save_HDLD()
        {
            if (radGridView2.Rows.Count > 0)
            {
                Conn = prs.Connect_HRP();
                Conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                   
                    if (radGridView2.Rows[0].Cells["LoaiHD_HDLD"].Value.ToString() != "Khong thoi han")
                    {
                        cmd.CommandText = "Ins_HopDong";
                        //MessageBox.Show("aaa");
                        for (i = radGridView2.Rows.Count - 1; i >= 0; i--)
                        {
                            if ((bool)radGridView2.Rows[i].Cells[0].Value == true)
                            {
                                MaNV1 = radGridView2.Rows[i].Cells["MaNV_HDLD"].Value.ToString();
                                Ho1 = radGridView2.Rows[i].Cells["HovaTen_HDLD"].Value.ToString();
                                ChucVu1 = radGridView2.Rows[i].Cells["NgayVao_HDLD"].Value.ToString();
                                NgayHD1 = Ngay_Split(radGridView2.Rows[i].Cells["DateBegin_HDLD"].Value.ToString());
                                MucLuong1 = radGridView2.Rows[i].Cells["Luong_HDLD"].Value.ToString();
                                LoaiHD1 = radGridView2.Rows[i].Cells["LoaiHD_HDLD"].Value.ToString();
                                HDLD1 = radGridView2.Rows[i].Cells["SoHD_HDLD"].Value.ToString();
                                PhuCap1 = radGridView2.Rows[i].Cells["PhuCap_HDLD"].Value.ToString();
                                PhuLuc1 = radGridView2.Rows[i].Cells["PhuLuc_HDLD"].Value.ToString();
                                BacLuong1 = radGridView2.Rows[i].Cells["BacLuong_HDLD"].Value.ToString();
                                NgayHD1 = Ngay_Split(radGridView2.Rows[i].Cells["DateBegin_HDLD"].Value.ToString());
                                NgayHH1 = Ngay_Split(radGridView2.Rows[i].Cells["DateEnd_HDLD"].Value.ToString());
                                cmd.Parameters.AddWithValue("@SoHD", HDLD1);
                                cmd.Parameters.AddWithValue("@MaNV", MaNV1);
                                cmd.Parameters.AddWithValue("@LoaiHD", LoaiHD1);
                                cmd.Parameters.AddWithValue("@NguoiSDLD", txt_NguoiDaiDien_HD.Text);
                                cmd.Parameters.AddWithValue("@NgayHD", NgayHD1);
                                cmd.Parameters.AddWithValue("@NgayHH", NgayHH1);
                                cmd.Parameters.AddWithValue("@Luong", MucLuong1);
                                cmd.Parameters.AddWithValue("@BacLuong", BacLuong1);
                                cmd.Parameters.AddWithValue("@LuongPC", PhuCap1);
                                cmd.Parameters.AddWithValue("@PhuLuc", PhuLuc1);
                                cmd.Parameters.AddWithValue("@UserNhap", lb_HovaTen.Text);
                                cmd.ExecuteNonQuery();
                                radGridView2.Rows.RemoveAt(i);
                                cmd.Parameters.Clear();
                            }
                        }
                    }
                    else
                    {
                        cmd.CommandText = "Ins_HopDongKoTH";
                       
                        for (i = radGridView2.Rows.Count - 1; i >= 0; i--)
                        {
                            if ((bool)radGridView2.Rows[i].Cells[0].Value == true)
                            {
                              
                                MaNV1 = radGridView2.Rows[i].Cells["MaNV_HDLD"].Value.ToString();
                                Ho1 = radGridView2.Rows[i].Cells["HovaTen_HDLD"].Value.ToString();
                                //ChucVu1 = radGridView2.Rows[i].Cells["NgayVao_HDLD"].Value.ToString();
                                NgayHD1 = Ngay_Split(radGridView2.Rows[i].Cells["DateBegin_HDLD"].Value.ToString());
                               
                                MucLuong1 = radGridView2.Rows[i].Cells["Luong_HDLD"].Value.ToString();
                                LoaiHD1 = radGridView2.Rows[i].Cells["LoaiHD_HDLD"].Value.ToString();
                                HDLD1 = radGridView2.Rows[i].Cells["SoHD_HDLD"].Value.ToString();
                                
                                PhuCap1 = radGridView2.Rows[i].Cells["PhuCap_HDLD"].Value.ToString();
                                PhuLuc1 = radGridView2.Rows[i].Cells["PhuLuc_HDLD"].Value.ToString();
                             
                                cmd.Parameters.AddWithValue("@SoHD", HDLD1);
                                cmd.Parameters.AddWithValue("@MaNV", MaNV1);
                                cmd.Parameters.AddWithValue("@LoaiHD", LoaiHD1);
                                cmd.Parameters.AddWithValue("@NguoiSDLD", txt_NguoiDaiDien_HD.Text);
                                cmd.Parameters.AddWithValue("@NgayHD", NgayHD1);
                               
                                cmd.Parameters.AddWithValue("@Luong", MucLuong1);
                                cmd.Parameters.AddWithValue("@LuongPC", PhuCap1);
                                cmd.Parameters.AddWithValue("@PhuLuc", PhuLuc1);
                                cmd.Parameters.AddWithValue("@UserNhap", lb_HovaTen.Text);
                               
                                cmd.ExecuteNonQuery();
                                radGridView2.Rows.RemoveAt(i);
                                cmd.Parameters.Clear();
                            }
                        }
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                }
                catch (Exception ex)
                {
                    string s = "insert duplicate key";
                    string FK = "FOREIGN KEY constraint";
                    string s1 = ex.Message.ToString();
                    if (s1.Contains(s))
                    { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                    else if (s1.Contains(FK))
                    { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                    DialogResult Err;
                    Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_save_HDLD_Click(object sender, EventArgs e)
        {
            save_HDLD();
        }
        private void cb_BacLuong_HDLD_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (cb_BacLuong_HDLD.Text != "")
                txt_Luong_HDLD.Text = prs.substring_right(cb_BacLuong_HDLD.Text, child);
        }
        #endregion
        #region //Thong Tin Hop Dong Lao Dong
        private void Search_HDLD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            try
            {
                string_temp = @"select ThongTinNV.MaNV,ThongTinNV.Ho+' '+ThongTinNV.Ten as HovaTen,HDLD.SoHD,convert(varchar(10),HDLD.NgayHD,103) as NgayHD,convert(varchar(10),HDLD.NgayHH,103) as NgayHH,HDLD.Luong,PhuLucHD.SoPLHD as PhuLuc,HDLD.LoaiHD+' - '+cast(LoaiHD.MucDo as nvarchar) as LoaiHD,ThongTinNV.HinhAnh,HDLD.LuongPC
                from HDLD inner join dbo.LoaiHD on LoaiHD.LoaiHD=HDLD.LoaiHD inner join ThongTinNV on ThongTinNV.MaNV=HDLD.MaNV left join PhuLucHD on PhuLucHD.SoHD=HDLD.SoHD where ThongTinNV.HoatDong=1";

                if (txt_MaNV_TTHDLD.Text != "")
                    string_temp += @" and dbo.ThongTinNV.MaNV like '%" + txt_MaNV_TTHDLD.Text + "%'";
                if (txt_HovaTen_TTHDLD.Text != "")
                    string_temp += " and ThongTinNV.Ten like '" + txt_HovaTen_TTHDLD.Text + "%'";
                if (txt_SoHD_TTHDLD.Text != "")
                    string_temp += " and HDLD.SoHD like '%" + txt_SoHD_TTHDLD.Text + "%'";
                if (cb_LoaiHD_TTHDLD.Text != "")
                    string_temp += " and HDLD.LoaiHD like '%" + prs.substring_left(cb_LoaiHD_TTHDLD.Text,child) + "%'";
                if (txt_DateBegin_TTHDLD.Text != "")
                {
                    NgayHD1 = Date_Split(txt_DateBegin_TTHDLD.Text);
                    string_temp += " and HDLD.NgayHD >='" + NgayHD1 + "'";
                }
                if (txt_DateEnd_TTHDLD.Text != "")
                {
                    NgayHH1 = Date_Split(txt_DateEnd_TTHDLD.Text);
                    string_temp += " and HDLD.NgayHH<='" + NgayHH1 + "'";
                }

                //if (cb_PhongBan.Text != "")
               // MessageBox.Show(string_temp);
                DataSet ds = new DataSet();
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    radGridView3.DataSource = ds.Tables[0];
                   // dataGridView1.DataSource = ds.Tables[0];
                    //Display_TTNV(dataGridView1.Rows.Count - 2);
                    //lb_index.Text = (dataGridView1.Rows.Count - 2).ToString();
                    txt_MaNV_TTHDLD.ReadOnly = true;
                    txt_SoHD_TTHDLD.ReadOnly = true;
                    bt_edit_TTHDLD.Enabled = true;
                    bt_delete_TTHDLD.Enabled = true;
                    //bt_SaveTTNV.Enabled = false;
                    // MessageBox.Show(ds.Tables[0].Rows[0]["HinhAnh"].GetType().ToString());
                    //MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
                }
                else
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void bt_search_TTHDLD_Click(object sender, EventArgs e)
        {
            Search_HDLD();
        }
        private void bt_reset_TTHDLD_Click(object sender, EventArgs e)
        {
            bt_delete_HDLDNLD.Enabled = false;
            bt_edit_HDLDNLD.Enabled = false;
            txt_MaNV_TTHDLD.ReadOnly = false;
            txt_SoHD_TTHDLD.ReadOnly = false;
            txt_MaNV_TTHDLD.Text = "";
            txt_Luong_TTHDLD.Text = "";
            txt_PhuCap_TTHDLD.Text = "";
            txt_SoHD_TTHDLD.Text = "";
            txt_DateBegin_TTHDLD.Text = "";
            txt_DateEnd_TTHDLD.Text = "";
            cb_LoaiHD_TTHDLD.Text = "";
            txt_HovaTen_TTHDLD.Text = "";
            pictureBox4.Image = null;
            
        }
        private void Display_TTHDLD(int row)
        {
            lb_index.Text = row.ToString();
            txt_MaNV_TTHDLD.Text = radGridView3.Rows[row].Cells["MaNV_HDLD"].Value.ToString();
            txt_HovaTen_TTHDLD.Text = radGridView3.Rows[row].Cells["HovaTen_HDLD"].Value.ToString();
            txt_SoHD_TTHDLD.Text = radGridView3.Rows[row].Cells["SoHD_HDLD"].Value.ToString();
            cb_LoaiHD_TTHDLD.Text = radGridView3.Rows[row].Cells["LoaiHD_HDLD"].Value.ToString();
            txt_DateBegin_TTHDLD.Text = radGridView3.Rows[row].Cells["DateBegin_HDLD"].Value.ToString();
            txt_DateEnd_TTHDLD.Text = radGridView3.Rows[row].Cells["DateEnd_HDLD"].Value.ToString();
            txt_Luong_TTHDLD.Text = radGridView3.Rows[row].Cells["Luong_HDLD"].Value.ToString();
            txt_PhuCap_TTHDLD.Text = radGridView3.Rows[row].Cells["PhuCap_HDLD"].Value.ToString();
           
            if (DBNull.Value.Equals(radGridView3.Rows[row].Cells["HinhAnh_HDLD"].Value))
            {
                //MessageBox.Show("aaa");
                pictureBox4.Image = null;
            }
            else
            {

                //MessageBox.Show("bbb");
                byte[] imageData = (byte[])radGridView3.Rows[i].Cells["HinhAnh_HDLD"].Value;
                //MessageBox.Show(dataGridView1.Rows[row].Cells["HinhAnh"].Value.GetType().ToString());
                //Initialize image variable
                Image newImage;
                //Read image data into a memory stream
                using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                {
                    ms.Write(imageData, 0, imageData.Length);

                    //Set image variable value using memory stream.
                    newImage = Image.FromStream(ms, true);
                }

                //set picture
                pictureBox4.Image = newImage;
            }
        }
        private void radGridView3_Click(object sender, EventArgs e)
        {
            try
            {
                i = radGridView3.CurrentRow.Index;
                // MessageBox.Show(i.ToString());
                //i=radGridView3.CurrentRow.ro

                Display_TTHDLD(i);
            }
            catch
            {
            }
        }
        private void bt_first_TTHDLD_Click(object sender, EventArgs e)
        {
            if (radGridView3.Rows.Count > 0)
            {
                Display_TTHDLD(0);
                lb_index.Text = "0";
            }
        }

        private void bt_last_TTHDLD_Click(object sender, EventArgs e)
        {
            if (radGridView3.Rows.Count > 0)
            {
                Display_TTHDLD(radGridView3.Rows.Count - 1);
                lb_index.Text = (radGridView3.Rows.Count - 1).ToString();
            }
        }

        private void bt_prev_TTHDLD_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView3.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_TTHDLD(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_TTHDLD_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView3.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < radGridView3.Rows.Count - 1)
                    {
                        i++;
                        Display_TTHDLD(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void edit_HDLD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Upd_HopDongLD";
            try
            {
                if (txt_MaNV_TTHDLD.Text == "" || txt_SoHD_TTHDLD.Text == "" || cb_LoaiHD_TTHDLD.Text == "" || txt_Luong_TTHDLD.Text == "")
                    MessageBox.Show("Kiểm tra lại thông tin");
                else
                {
                    DialogResult TL;
                    TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SỐ HỢP ĐỒNG " + txt_SoHD_TTHDLD.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (TL == DialogResult.Yes)
                    {
                        LoaiHD1 = prs.substring_left(cb_LoaiHD_TTHDLD.Text, child);
                        if (LoaiHD1 == "")
                            MessageBox.Show("Nhập sai hợp đồng");
                        else
                        {
                            if (txt_PhuCap_TTHDLD.Text == "")
                                PhuCap1 = "0";
                            else
                                PhuCap1 = double.Parse(txt_PhuCap_TTHDLD.Text).ToString();
                            cmd.Parameters.AddWithValue("@LoaiHD", LoaiHD1);
                            cmd.Parameters.AddWithValue("@Luong", double.Parse(txt_Luong_TTHDLD.Text));
                            cmd.Parameters.AddWithValue("@LuongPC", PhuCap1);
                            if (LoaiHD1 != "Khong thoi han")
                            {
                                if (txt_DateEnd_TTHDLD.Text == "" || txt_DateBegin_TTHDLD.Text == "")
                                    MessageBox.Show("Nhập thiếu ngày");
                                else
                                {
                                    NgayHD1 = Date_Split(txt_DateBegin_TTHDLD.Text);
                                    NgayHH1 = Date_Split(txt_DateEnd_TTHDLD.Text);
                                    cmd.Parameters.AddWithValue("@NgayHH", NgayHH1);
                                    cmd.Parameters.AddWithValue("@check", "1");
                                }

                            }
                            else
                            {
                                NgayHD1 = Date_Split(txt_DateBegin_TTHDLD.Text);
                                NgayHH1 = "01/01/2100";
                                cmd.Parameters.AddWithValue("@check", "0");
                            }
                            cmd.Parameters.AddWithValue("@NgayHH", NgayHH1);
                            cmd.Parameters.AddWithValue("@NgayHD", NgayHD1);
                            cmd.Parameters.AddWithValue("@SoHD", txt_SoHD_TTHDLD.Text);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("CHỈNH SỬA THÀNH CÔNG");
                            bt_reset_TTHDLD_Click(null, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("KIỂM TRA LẠI THÔNG TIN NHẬP, SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void txt_Luong_TTHDLD_Leave(object sender, EventArgs e)
        {
            checktext_number(txt_Luong_TTHDLD);
        }
        private void txt_PhuCap_TTHDLD_Leave(object sender, EventArgs e)
        {
            checktext_number(txt_PhuCap_TTHDLD);
        }
        private void bt_edit_TTHDLD_Click(object sender, EventArgs e)
        {
            edit_HDLD();
        }
        private void delete_HDLD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA SỐ HỢP ĐỒNG "+txt_SoHD_TTHDLD.Text+" KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (TL == DialogResult.Yes)
                {
                    cmd.CommandText = "delete from dbo.HDLD where SoHD=N'" + txt_SoHD_TTHDLD.Text.Trim() + "'";
                   // MessageBox.Show(cmd.CommandText);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    bt_reset_TTHDLD_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("KIỂM TRA LẠI THÔNG TIN NHẬP, XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_delete_TTHDLD_Click(object sender, EventArgs e)
        {
            delete_HDLD();
        }
        #endregion
        #region //Phu Luc Hop Dong
        private void save_PLHD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Ins_PLHD";
            try
            {
                if (txt_SoHD_PLHD.Text == "" || txt_PhuLuc_PLHD.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    cmd.Parameters.AddWithValue("@SoHD", txt_SoHD_PLHD.Text.Trim());
                    cmd.Parameters.AddWithValue("@SoPLHD", txt_SoHD_PLHD.Text.Trim());
                    cmd.Parameters.AddWithValue("@GhiChu", txt_GhiChu_PLHD.Text);
                    cmd.Parameters.AddWithValue("@Luong", double.Parse(txt_Luong_PLHD.Text));
                    cmd.Parameters.AddWithValue("@UserNhap", lb_HovaTen.Text);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_reset_PLHD_Click(null, null);                    
                }                                          
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_reset_PLHD_Click(object sender, EventArgs e)
        {
            txt_GhiChu_PLHD.Text = "";
            txt_PhuLuc_PLHD.Text = "";
            txt_SoHD_PLHD.Text = "";
            txt_Luong_PLHD.Text = "";
            bt_delete_PLHD.Enabled = false;
            bt_edit_PLHD.Enabled = false;
        }
        private void Display_PLHD(int row)
        {
            try
            {
                lb_index.Text = row.ToString();
                txt_SoHD_PLHD.Text = dataGridView2.Rows[row].Cells["SoHD"].Value.ToString();
                txt_PhuLuc_PLHD.Text = dataGridView2.Rows[row].Cells["SoPLHD"].Value.ToString();
                txt_Luong_PLHD.Text = dataGridView2.Rows[row].Cells["Luong"].Value.ToString();
                txt_GhiChu_PLHD.Text = dataGridView2.Rows[row].Cells["GhiChu"].Value.ToString();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bt_prev_PLHD_Click(object sender, EventArgs e)
        {
            try
            {

                if (dataGridView2.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_PLHD(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_PLHD_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView2.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView2.Rows.Count - 2)
                    {
                        i++;
                        Display_PLHD(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void search_PLHD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            try
            {
                if(txt_SoHD_PLHD.Text!="")
                    string_temp = @"select * from dbo.PhuLucHD where SoHD = N'"+txt_SoHD_PLHD.Text.Trim()+"'";
                

                
                
                DataSet ds = new DataSet();
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0];
                    Display_PLHD(dataGridView2.Rows.Count - 2);
                    bt_delete_PLHD.Enabled = true;
                    bt_edit_PLHD.Enabled = true;
                    lb_index.Text = (dataGridView2.Rows.Count - 2).ToString();
                    
                }
                else
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        
        private void bt_search_PLHD_Click(object sender, EventArgs e)
        {
            search_PLHD();
        }
      

        private void txt_Luong_PLHD_Leave(object sender, EventArgs e)
        {
            checktext_number(txt_Luong_PLHD);

        }
        private void edit_PLHD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                if (txt_PhuLuc_PLHD.Text==""||txt_SoHD_PLHD.Text=="")
                    MessageBox.Show("Kiểm tra lại thông tin");
                else
                {
                    DialogResult TL;
                    TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SỐ PHỤ LỤC HỢP ĐỒNG " + txt_PhuLuc_PLHD.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (TL == DialogResult.Yes)
                    {
                        cmd.CommandText = @"UPDATE [dbo].[PhuLucHD] SET SoHD=N'"+txt_SoHD_PLHD.Text.Trim()+"',Luong =" + double.Parse(txt_Luong_PLHD.Text) + ",GhiChu =N'" + txt_GhiChu_PLHD.Text + "' WHERE SoPLHD=N'" + txt_PhuLuc_PLHD.Text.Trim() + "'";
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("CHỈNH SỬA THÀNH CÔNG");
                        bt_reset_PLHD_Click(null, null);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("KIỂM TRA LẠI THÔNG TIN NHẬP, SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void delete_PLHD()
        {
            Conn = prs.Connect_HRP();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA SỐ PHỤ LỤC HỢP ĐỒNG " + txt_PhuLuc_PLHD.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (TL == DialogResult.Yes)
                {
                    cmd.CommandText = "delete from dbo.PhuLucHD where SoPLHD=N'" + txt_PhuLuc_PLHD.Text + "'";
                    //MessageBox.Show(cmd.CommandText);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    bt_reset_PLHD_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("KIỂM TRA LẠI THÔNG TIN NHẬP, XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_save_PLHD_Click(object sender, EventArgs e)
        {
            save_PLHD();
        }

        private void bt_edit_PLHD_Click(object sender, EventArgs e)
        {
            edit_PLHD();
        }

        private void bt_delete_PLHD_Click(object sender, EventArgs e)
        {
            delete_PLHD();
        }
        private void radLabel87_DoubleClick(object sender, EventArgs e)
        {
            if (txt_SoHD_PLHD.Text != "")
            {
                Conn = prs.Connect_HRP();
                Conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                cmd.CommandText = "select cast(DATEPART(MM,getdate()) as nvarchar)+CAST((datepart(YY,getdate())-2000) as nvarchar)";
                try
                {
                    string_temp = cmd.ExecuteScalar().ToString();

                    string[] temp = txt_SoHD_PLHD.Text.Split('/');
                    if (string_temp.Length == 3)
                        string_temp = "0" + string_temp;
                    txt_PhuLuc_PLHD.Text = temp[0] + "/" + "PLHĐLĐ/SP/" + string_temp;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }

        #endregion
        #region //In Hợp đồng
        private void print_HDLD()
        {
            string_temp = @"SELECT ThongTinNV.[MaNV] ,ThongTinNV.Ho+' '+ThongTinNV.Ten as HovaTen,HDLD.SoHD,[LoaiHD],convert(varchar(10),[NgayHD],103) as TuNgay,convert(varchar(10),[NgayHH],103) as DenNgay,[Luong],[LuongPC],[PhuLuc],ThongTinNV.QuocTich,convert(varchar(10),ThongTinNV.NgaySinh,103) as NgaySinh,ThongTinNV.NoiSinh,ThongTinNV.ThuongTru,ThongTinNV.TamTru,ThongTinNV.CMND,convert(varchar(10),ThongTinNV.NgayCap,103) as NgayCap,ThongTinNV.NoiCap,ThongTinNV.Email,CongTy.ChuyenMon,CongTy.MST,CongTy.SoLD,CongTy.ChucVu,'ngày '+cast(DATEPART(DD,NgayHD) as nvarchar)+' tháng '+CAST(datepart(MM,NgayHD) as nvarchar)+N' năm '+CAST(datepart(yyyy,NgayHD) as nvarchar) as temp,'ngày '+cast(DATEPART(DD,NgayHH) as nvarchar)+' tháng '+CAST(datepart(MM,NgayHH) as nvarchar)+N' năm '+CAST(datepart(yyyy,NgayHH) as nvarchar) as Fax,round(Luong/26,0) as SoTK,HDLD.BacLuong as GhiChu
                            FROM [HRP_ScansiaPacific].[dbo].[HDLD] inner join ThongTinNV on ThongTinNV.MaNV=HDLD.MaNV inner join dbo.CongTy on CongTy.MaNV=ThongTinNV.MaNV where ThongTinNV.HoatDong=1 ";
            Conn = prs.Connect_HRP();
            Conn.Open();
            DataSet ds = new DataSet();
            try
            {
                if (txt_MaNV_reportHDLD.Text != "")
                    string_temp += " and ThongTinNV.MaNV='"+txt_MaNV_reportHDLD.Text.Trim()+"'";
                if (txt_SoHD_reportHDLD.Text != "")
                    string_temp += " and HDLD.SoHD='" + txt_SoHD_reportHDLD.Text.Trim() + "'";
                if (cb_LoaiHD_reportHDLD.Text != "")
                    string_temp += " and HDLD.LoaiHD=N'" + prs.substring_left(cb_LoaiHD_reportHDLD.Text, child) + "'";
                if (txt_DateBegin_reportHDLD.Text != "")
                    string_temp += " and NgayHD>='"+Date_Split(txt_DateBegin_reportHDLD.Text)+"'";
                if (txt_DateEnd_reportHDLD.Text != "")
                    string_temp += " and NgayHH>='" + Date_Split(txt_DateEnd_reportHDLD.Text) + "'";
                ds = prs.dts(string_temp, Conn, ds);
                radGridView4.DataSource = ds.Tables[0];
                chk_selectall_reportHDLD_ToggleStateChanged(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Đã xảy ra lỗi, vui lòng kiểm tra thông tin đầu vào",ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void bt_search_reportHDLD_Click(object sender, EventArgs e)
        {
            print_HDLD();
        }
        private void chk_selectall_reportHDLD_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (radGridView4.Rows.Count > 0)
            {
                radGridView4.CurrentRow = radGridView4.Rows[0];
                radGridView4.GridElement.ScrollToRow(0);
                if (chk_selectall_reportHDLD.Checked == true)
                {

                    //lb_Kien.Text = "0";
                    for (i = 0; i < radGridView4.Rows.Count; i++)
                    {
                        //if ((bool)radGridView4.Rows[i].Cells[0].Value == false)
                        radGridView4.Rows[i].Cells[0].Value = true;
                        //count++;
                    }

                }
                if (chk_selectall_reportHDLD.Checked == false)
                {

                    for (i = 0; i < radGridView4.Rows.Count; i++)
                    {

                        radGridView4.Rows[i].Cells[0].Value = false;

                    }

                }
            }
        }
        private void bt_check_reportHDLD_Click(object sender, EventArgs e)
        {
            if (radGridView4.Rows.Count > 0)
            {
                try
                {
                    
                    for (i = radGridView4.Rows.Count - 1; i >= 0; i--)
                    {
                        if ((bool)radGridView4.Rows[i].Cells[0].Value == false)
                        {
                            
                            radGridView4.Rows.RemoveAt(i);
                            
                            //radGridView4.Rows.
                        }
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void bt_print_reportHDLD_Click(object sender, EventArgs e)
        {
            if (radGridView4.Rows.Count > 0)
            {
                if (cb_LoaiHD_reportHDLD.Text != "")
                {
                    //MessageBox.Show(radGridView4.Columns.Count.ToString());
                    DataTable dt = new DataTable();
                    for (i = 0; i < radGridView4.Columns.Count; i++)
                    {

                        dt.Columns.Add(radGridView4.Columns[i].FieldName.ToString());

                        //MessageBox.Show(radGridView4.Columns[i].FieldName.ToString());
                    }
                    
                    for (i = 0; i < radGridView4.Rows.Count; i++)
                    {
                        DataRow row = dt.NewRow();
                        for (j = 0; j < radGridView4.Columns.Count; j++)
                        {
                            row[j] = radGridView4.Rows[i].Cells[j].Value;
                        }
                        dt.Rows.Add(row);

                    }
                    
                    string_temp = prs.substring_left(cb_LoaiHD_reportHDLD.Text, child);
                    Fr_Report FrR = new Fr_Report();

                    if (string_temp == "Khong thoi han")
                    {
                        FrR.report_HDLD_KTH(dt, txt_NguoiDaiDien_HD.Text, txt_ChucVu_NDD.Text);
                    }
                    if (string_temp == "Thoi vu")
                    {
                        FrR.report_HDLD_TV(dt, txt_NguoiDaiDien_HD.Text, txt_ChucVu_NDD.Text);
                    }
                    if (string_temp == "Mot nam")
                    {
                        FrR.report_HDLD_1Nam(dt, txt_NguoiDaiDien_HD.Text, txt_ChucVu_NDD.Text);
                    }
                    //FrR.report_HDLD_1Nam(dt, txt_NguoiDaiDien_HD.Text, txt_ChucVu_NDD.Text);
                    // FrR.report_KV1NKTG(ds, Ngay1);
                    FrR.Show();
                    dt.Clear();
                }
                else
                    MessageBox.Show("Vui lòng nhập loại hợp đồng");
            }
        }
        #endregion

        private void Mni_LoadData_Click(object sender, EventArgs e)
        {
            Fr_HuMan_LoadData FrLoad = new Fr_HuMan_LoadData();
            
            FrLoad.TopLevel = false;
            FrLoad.Location = new Point(0, 20);
            FrLoad.Parent = Page_CCTinhLuong;
            FrLoad.Show();
        }

        

       
    }
}
